home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume12 / strings.coff < prev    next >
Encoding:
Internet Message Format  |  1987-10-25  |  12.6 KB

  1. Subject:  v12i048:  Find printable strings in COFF files
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rs@uunet.UU.NET
  5.  
  6. Submitted-by: Tom Reynolds <kiwi!reynolds>
  7. Posting-number: Volume 12, Issue 48
  8. Archive-name: strings.coff
  9.  
  10. Strings finds ascii text strings in binary files, similar to the
  11. Berkely strings(8) program.  This strings understands the Common
  12. Object File Format (COFF) used by System V.
  13.  
  14. [  The Makefile will have to be tweaked for non-Microport Unices.  --r$  ]
  15.  
  16. #! /bin/sh
  17. # This is a shell archive, meaning:
  18. # 1. Remove everything above the #! /bin/sh line.
  19. # 2. Save the resulting text in a file.
  20. # 3. Execute the file with /bin/sh (not csh) to create:
  21. #    Readme   Makefile   strings.c   strings.8
  22. export PATH; PATH=/bin:/usr/bin:$PATH
  23. echo shar: "extracting 'Readme'" '(303 characters)'
  24. if test -f 'Readme'
  25. then
  26.     echo shar: "will not over-write existing file 'Readme'"
  27. else
  28. sed 's/^X//' << \SHAR_EOF > 'Readme'
  29. XStrings  has been  developed  and tested  on  a AT-clone  running
  30. XMicroport  System   V/AT  2.2  but   should  work  on   any  SysV
  31. Xenvironment.
  32. X
  33. X-------
  34. X
  35. XTom Reynolds                voice: (205) 721-1200 x 303
  36. XPhoenix microsystems, inc.        uucp:  ..!uunet!ingr!kiwi!reynolds
  37. X991 Discovery Drive
  38. XHuntsville, AL 35806
  39. SHAR_EOF
  40. if test 303 -ne "`wc -c < 'Readme'`"
  41. then
  42.     echo shar: "error transmitting 'Readme'" '(should have been 303 characters)'
  43. fi
  44. fi
  45. echo shar: "extracting 'Makefile'" '(430 characters)'
  46. if test -f 'Makefile'
  47. then
  48.     echo shar: "will not over-write existing file 'Makefile'"
  49. else
  50. sed 's/^X//' << \SHAR_EOF > 'Makefile'
  51. XCFLAGS=-Ml -O
  52. XLDFLAGS=-Ml
  53. Xstrings: strings.c
  54. X    cc -o strings $(LDFLAGS) strings.c -lld
  55. Xinstall: strings
  56. X    strip strings
  57. X    mv strings /usr/local/bin
  58. X    cd /usr/local/bin; chmod 755 strings; chgrp sys strings; chown root strings
  59. X    cp strings.8 /usr/man/man8/strings.8
  60. X    cd /usr/man/man8; chmod 755 strings.8; chgrp sys strings.8; chown root strings.8
  61. X    touch install
  62. Xstrings.shar:
  63. X    shar -a Readme Makefile strings.c strings.8 >strings.shar
  64. SHAR_EOF
  65. if test 430 -ne "`wc -c < 'Makefile'`"
  66. then
  67.     echo shar: "error transmitting 'Makefile'" '(should have been 430 characters)'
  68. fi
  69. fi
  70. echo shar: "extracting 'strings.c'" '(7898 characters)'
  71. if test -f 'strings.c'
  72. then
  73.     echo shar: "will not over-write existing file 'strings.c'"
  74. else
  75. sed 's/^X//' << \SHAR_EOF > 'strings.c'
  76. X/*========================================================================*
  77. X * STRINGS(8) -- Find strings in COFF files                               *
  78. X *                                                                        *
  79. X * Copyright (c) 1987                                                     *
  80. X * Tom Reynolds                                                           *
  81. X * Phoenix microsystems, inc.                                             *
  82. X * 991 Discovery Drive                                                    *
  83. X * Huntsville, AL 35807                                                   *
  84. X *                                                                        *
  85. X * Permission granted to publish in source and object form.               *
  86. X * Publication must retain all copyright notices.                         *
  87. X * Object-only distribution permitted only if source is also              *
  88. X * available from distributor.                                            *
  89. X * Fees are limited to cost-of-media charges.                             *
  90. X *                                                                        *
  91. X *========================================================================*/
  92. X
  93. X#include <stdio.h>
  94. X#include <filehdr.h>
  95. X#include <scnhdr.h>
  96. X#include <ldfcn.h>
  97. X#include <varargs.h>
  98. X#include <ctype.h>
  99. X
  100. X#define OPTIONS    "an:o"            /* GETOPT(3C) Option string */
  101. X
  102. Xchar *copyright = 
  103. X"Copyright (c) 1987 by Tom Reynolds, Phoenix microsystems, inc. Huntsville AL";
  104. X
  105. Xextern char
  106. X   *optarg;                /* GETOPT(3C): Current argument */
  107. X
  108. Xextern int
  109. X   getopt(),                /* GETOPT(3C): Declaration */
  110. X   optind;                /* GETOPT(3C): argv[0] index */
  111. X
  112. Xextern long 
  113. X   ftell();                /* FTELL() calls this */
  114. X
  115. Xchar
  116. X   *fn,                    /* Current file name being processed */
  117. X   *me,                    /* Process name (from argv[0]) */
  118. X   *options = OPTIONS;            /* Command line option string */
  119. X
  120. Xint
  121. X   length = 4,                /* Need this many to make string */
  122. X   multi = 0;                /* True if more than 1 input file */
  123. X   offset = 0;                /* True if file offset wanted */
  124. X
  125. Xlong
  126. X   types = STYP_DATA;            /* Section types to search */
  127. X
  128. XLDFILE
  129. X   *ldptr;                /* COFF File pointer */
  130. X
  131. X/*========================================================================*
  132. X * error: Write fatal error text and exit                                 *
  133. X *========================================================================*/
  134. X
  135. X/*VARARGS*/
  136. Xvoid
  137. Xerror( va_alist )
  138. Xva_dcl
  139. X{
  140. X   va_list args;            /* Argument list keeper */
  141. X   char *fmt;                /* Printf()-like format address */
  142. X   va_start( args );            /* Init the varargs mechanism */
  143. X   fmt = va_arg( args, char * );    /* Pick off format */
  144. X   (void) fprintf( stderr, "%s: ", me );/* Write our process name */
  145. X   (void) vfprintf( stderr, fmt, args );/* Write message text */
  146. X   (void) fputc( '\n', stderr );    /* Terminate the line */
  147. X   va_end( args );            /* Clean up stack */
  148. X   exit( 1 );                /* Back to real world */
  149. X}
  150. X/*ARGSUSED*/
  151. X
  152. X/*========================================================================*
  153. X * usage: Tell how to use us                                              *
  154. X *========================================================================*/
  155. X
  156. Xvoid
  157. Xusage( s )
  158. Xchar *s;                /* Optional error text */
  159. X{
  160. X   if( s != (char *) NULL ) (void) fprintf( stderr, "%s: %s\n", me, s );
  161. X   (void) fprintf( stderr, "usage: %s [-a] [-n #] [-o] file...\n", me );
  162. X   (void) exit( 1 );
  163. X}
  164. X
  165. X/*========================================================================*
  166. X * strings: Find and output all strings in this section of file           *
  167. X *========================================================================*/
  168. X
  169. Xvoid
  170. Xstrings( cnt )
  171. Xlong cnt;                /* Bytes in this section */
  172. X{
  173. X   register int c;            /* Character we're considering */
  174. X   char buf[ BUFSIZ+1 ];        /* Holding area for string */
  175. X   register char *bp;            /* Walks down buf[] */
  176. X   register char *eob = buf+BUFSIZ;    /* End of buffer */
  177. X   int n;                /* Length of our string in buf[] */
  178. X   for( bp = buf; cnt != 0L; cnt-- ) {
  179. X      c = GETC( ldptr );        /* Get a character */
  180. X      if( c == -1 ) break;        /* Get out if end of file */
  181. X      if( isprint( c ) ) {
  182. X     /* Found a printable character */
  183. X     if( bp != eob ) *bp++ = c;
  184. X      } else if( (c == '\0') || (c == '\n') ) {
  185. X     /* Found a proper terminator, so have string if long enough */
  186. X     n = bp - buf;            /* How long is it? */
  187. X     if( n >= length ) {
  188. X        /* String passes */
  189. X        *bp = '\0';            /* End the string */
  190. X        if( multi ) (void) printf( "%s\t", fn );
  191. X        if( offset ) 
  192. X           (void) printf( "%#lo\t", FTELL( ldptr ) - (long) n - 1L );
  193. X        (void) printf( "%s\n", buf ); /* Show it */
  194. X     }
  195. X     bp = buf;            /* Start string over again */
  196. X      } else bp = buf;            /* Strings doesn't end right */
  197. X   }
  198. X}
  199. X
  200. X/*========================================================================*
  201. X * process: Find strings in a single COFF object                          *
  202. X *========================================================================*/
  203. X
  204. Xvoid
  205. Xprocess()
  206. X{
  207. X   if( ISCOFF( TYPE( ldptr ) ) ) {
  208. X      unsigned short section;        /* Which section we're working on */
  209. X      struct scnhdr hdr;        /* Section header storage */
  210. X      long here;            /* Progress into file so far */
  211. X      if( FSEEK( ldptr, (long) HEADER( ldptr ).f_opthdr, 0 ) == -1 )
  212. X     error( "Could not skip optional header in file \"%s\"", fn );
  213. X      here = FTELL( ldptr ) + 
  214. X     (sizeof( struct scnhdr ) * HEADER( ldptr ).f_nscns);
  215. X      for( section = 1; section <= HEADER( ldptr ).f_nscns; section++ ) {
  216. X     if( ldshread( ldptr, section, &hdr ) == FAILURE )
  217. X        error( "Could not read section %d's section header in file \"%s\"",
  218. X           section, fn );
  219. X     if( FSEEK( ldptr, here, 0 ) == -1 ) {
  220. X        error( "Could not seek to data segment %d in file \"%s\"", 
  221. X           section, fn );
  222. X     }
  223. X     if( hdr.s_flags & types )
  224. X        strings( hdr.s_size );    /* Find the strings */
  225. X     else if( FSEEK( ldptr, hdr.s_size, 0 ) == -1 ) {
  226. X        error( "Could not step over section %d's data area in file \"%s\"",
  227. X           section, fn );
  228. X     }
  229. X     here = FTELL( ldptr );        /* Remember data for next section */
  230. X      }
  231. X   } else {
  232. X      /* Not a COFF file, so do the best we can */
  233. X      if( FSEEK( ldptr, 0L, 0 ) == -1 )
  234. X     error( "Could not seek beginning of file \"%s\"", fn );
  235. X      strings( -1L );
  236. X   }
  237. X}
  238. X
  239. X/*========================================================================*
  240. X * main: Central Control Logic                                            *
  241. X *========================================================================*/
  242. X
  243. Xint
  244. Xmain( argc, argv )
  245. Xint argc;                /* Number of arguments */
  246. Xchar **argv;                /* Command line arguments */
  247. X{
  248. X   int c;                /* Command line option character */
  249. X   int errflag = 0;            /* Command line error flag */
  250. X   me = argv[ 0 ];            /* Save our process name */
  251. X   while( (c = getopt( argc, argv, options )) != EOF ) {
  252. X      switch( c ) {
  253. X     default:  fprintf( stderr, "%s: No -%c yet!\n", me, c ); /*FALLTHRU*/
  254. X     case '?': errflag++; break;
  255. X     case 'a': types |= STYP_TEXT; break;
  256. X     case 'n': length = atoi( optarg ); break;
  257. X     case 'o': offset = 1; break;
  258. X      }
  259. X   }
  260. X   if( errflag >  0      ) usage( "Illegal switches" );
  261. X   if( length  <  1      ) usage( "-n spec too short" );
  262. X   if( length  >  BUFSIZ ) usage( "-n too long" );
  263. X   switch( argc - optind ) {
  264. X      case 0: usage( "Need input filenames" );        /* No files! */
  265. X      case 1: break;                    /* Only one file */
  266. X      default: multi = 1; break;            /* Lotsa files */
  267. X   }
  268. X   if( optind  >= argc   ) usage( "Need input filenames" );
  269. X   while( optind < argc ) {
  270. X      /* For every input file */
  271. X      fn = optarg = argv[ optind++ ];    /* Get another filename */
  272. X      ldptr = (LDFILE *) NULL;        /* Start clean */
  273. X      do {
  274. X     if( (ldptr = ldopen( fn, ldptr )) != (LDFILE *) NULL ) {
  275. X        process();
  276. X     } else {
  277. X        fprintf( stderr, "%s: Cannot open %s\n", me, fn );
  278. X        errflag++;
  279. X     }
  280. X      } while( ldclose( ldptr ) == FAILURE );
  281. X   }
  282. X   (void) exit( errflag ? 1 : 0 );
  283. X   /* NOTREACHED */
  284. X}
  285. SHAR_EOF
  286. if test 7898 -ne "`wc -c < 'strings.c'`"
  287. then
  288.     echo shar: "error transmitting 'strings.c'" '(should have been 7898 characters)'
  289. fi
  290. fi
  291. echo shar: "extracting 'strings.8'" '(1562 characters)'
  292. if test -f 'strings.8'
  293. then
  294.     echo shar: "will not over-write existing file 'strings.8'"
  295. else
  296. sed 's/^X//' << \SHAR_EOF > 'strings.8'
  297. X.TH STRINGS 8 
  298. X.SH NAME
  299. Xstrings \- find printable strings in files
  300. X.SH SYNOPSIS
  301. X.B strings 
  302. X.B " [ " -a " ] "
  303. X.B " [ " "-n\ #" " ] "
  304. X.B " [ " -o " ] "
  305. X.RB ... file ...
  306. X.SH DESCRIPTION
  307. X.PP
  308. X.B Strings
  309. Xdisplays ascii strings contained in the named input files.
  310. XThe input files may be either ordinary files or 
  311. X.B COFF
  312. Xobject files.
  313. XFor ordinary files, the entire file is searched for strings.
  314. XCOFF files have only their 
  315. X.B .data
  316. Xsections searched unless the
  317. X.B -a
  318. Xoption is used which also searches any
  319. X.B .text
  320. Xsections.
  321. X.PP
  322. XThe strings extracted by
  323. X.B strings
  324. Xare defined to be a sequence of at least 4 (by default) printable
  325. Xcharacters terminated by either a
  326. X.B NULL 
  327. Xor a
  328. X.BR NEWLINE .
  329. XThe
  330. X.B -n #
  331. Xoption changes the minimum number of characters.
  332. XFrom 1 to 
  333. X.B BUFSIZ
  334. Xcharacters are possible.
  335. X.PP
  336. XThe
  337. X.B -o 
  338. Xswitch causes 
  339. X.B strings
  340. Xto prefix each output line with the octal offset within the file
  341. X(which is probably not the COFF memory address).
  342. X.SH "IN MEMORIUM"
  343. X.B Strings
  344. Xhas been modeled after the Berkeley strings(1) tool.
  345. XIt is not a Berkeley product.
  346. X.SH COPYRIGHT
  347. X.PP
  348. X.B Strings
  349. Xis copyright 1987 by Tom Reynolds.
  350. XPermission is hereby granted to publish 
  351. X.B strings
  352. Xin source or object form as long as all copyright notices are
  353. Xretained.
  354. XObject\-only distributions are permitted only if the source is also
  355. Xfreely available from the distributor.
  356. XAny fee charged for such publication may consist only of a
  357. Xreasonable charge for any media used.
  358. X.SH AUTHOR
  359. X.PP
  360. XTom Reynolds
  361. X.br
  362. XPhoenix microsystems, inc.
  363. X.br
  364. X991 Discovery Drive
  365. X.br
  366. XHuntsville, AL 35806
  367. SHAR_EOF
  368. if test 1562 -ne "`wc -c < 'strings.8'`"
  369. then
  370.     echo shar: "error transmitting 'strings.8'" '(should have been 1562 characters)'
  371. fi
  372. fi
  373. exit 0
  374. #    End of shell archive
  375.  
  376. ---------
  377.  
  378. Tom Reynolds                voice: (205) 721-1200 x 303
  379. Phoenix microsystems, inc.        uucp:  ..!uunet!ingr!kiwi!reynolds
  380. 991 Discovery Drive
  381. Huntsville, AL 35806
  382. -- 
  383. Tom Reynolds                voice: (205) 721-1200 x 303
  384. Phoenix microsystems, inc.        uucp:  ..!uunet!ingr!kiwi!reynolds
  385. 991 Discovery Drive
  386. Huntsville, AL 35806
  387.  
  388.